package com.qs.eggs.view.prodetail.fragment;


import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.daimajia.slider.library.Animations.DescriptionAnimation;
import com.daimajia.slider.library.SliderLayout;
import com.daimajia.slider.library.SliderTypes.BaseSliderView;
import com.daimajia.slider.library.SliderTypes.DefaultSliderView;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.qs.eggs.Constants;
import com.qs.eggs.R;
import com.qs.eggs.account.LoginActivity;
import com.qs.eggs.adapter.DetailsAdapter;
import com.qs.eggs.adapter.ProductAdapter;
import com.qs.eggs.api.HttpApi;
import com.qs.eggs.base.fragment.BaseFragment;
import com.qs.eggs.view.activity.GonggaoActivity;
import com.qs.eggs.view.activity.SubmitOrderActivity;
import com.qs.eggs.view.prodetail.activity.ProDetailActivity;
import com.qs.eggs.widgets.custom.GridViewForScrollView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Response;

/**
 * A simple {@link Fragment} subclass.
 */
public class DetailsFragment extends BaseFragment {
    private RecyclerView recycleView;
    private String proid,type;
    private DetailsAdapter detailsAdapter;
    private String note;
    private WebView webView;
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_details;
    }

    @Override
    protected void initWidget(View root) {
        super.initWidget(root);
        proid=getActivity().getIntent().getStringExtra("proid");
        type=getActivity().getIntent().getStringExtra("type");
        webView=findView(R.id.webView);
        webView.getSettings().setJavaScriptEnabled(true);
        getPic();

    }
    //获得图文详情
   private void getPic(){
       HashMap<String,String> map=new HashMap<String, String>();
       map.put("proid",proid);
       map.put("type",type);
       Gson gson=new Gson();
       String str=gson.toJson(map);

       OkGo.post(Constants.URL_BASE+"mall/showproduct?action=productDetail")
               .params("data",str)
               .execute(new StringCallback() {
                   @Override
                   public void onSuccess(String s, Call call, Response response) {
                       System.out.print("图文详情--" + s);
                       try {
                           List<Map<String, Object>> piclist = new ArrayList<>();
                           JSONArray jsonArray = new JSONArray(s);
                           for (int i = 0; i < jsonArray.length(); i++) {
                               JSONObject jsonObject = jsonArray.getJSONObject(i);
                               note = jsonObject.getString("note");
                               webView.setWebViewClient(new MyWebViewClient());
                               webView.setWebChromeClient(new WebChromeClient());
                               webView.loadDataWithBaseURL("http://www.shouyaoguanjia.com/",note,"text/html","utf-8",null);
                           }
                       } catch (Exception e) {
                           e.printStackTrace();
                       }
                   }
               });
   }

    private class MyWebViewClient extends WebViewClient {

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            // html加载完成之后，添加监听图片的点击js函数
            //addImageClickListner();
            imgReset();
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }
    /**
     * 循环遍历标签中的图片
     * js 语法
     */
    private void imgReset() {
        webView.loadUrl("javascript:(function(){" +
                "var objs = document.getElementsByTagName('img'); " +
                "for(var i=0;i<objs.length;i++)  " +
                "{"
                + "var img = objs[i];   " +
                "    img.style.maxWidth = '100%';   " +
                "}" +
                "})()");
    }
}
